package com.knowledge.plus.自定义异常.controller;

import com.knowledge.plus.自定义异常.MyException;
import com.knowledge.plus.自定义异常.pojo.CustomUser;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试控制器
 *
 * @author PF.Tian
 * @since 2021/09/13
 */
@RestController
@RequestMapping(value = "/api")
public class CustomExceptionController {

    @PostMapping("/user")
    public boolean insert(@RequestBody CustomUser customUser) {
        System.out.println("开始新增...");
        //如果姓名为空就手动抛出一个自定义的异常！
        if (customUser.getName() == null) {
            throw new MyException("-1", "用户姓名不能为空！");
        }
        return true;
    }

    @PutMapping("/user")
    public boolean update(@RequestBody CustomUser customUser) {
        System.out.println("开始更新...");
        //这里故意造成一个空指针的异常，并且不进行处理
        String str = null;
        str.equals("111");
        return true;
    }

    @DeleteMapping("/user")
    public boolean delete(@RequestBody CustomUser customUser) {
        System.out.println("开始删除...");
        //这里故意造成一个异常，并且不进行处理
        Integer.parseInt("abc123");
        return true;
    }

    @GetMapping("/user")
    public List<CustomUser> findByUser(CustomUser customUser) {
        System.out.println("开始查询...");
        List<CustomUser> customUserList = new ArrayList<>();
        CustomUser customUser2 = new CustomUser();
        customUser2.setId(1);
        customUser2.setName("自定义异常");
        customUser2.setAge(18);
        customUserList.add(customUser2);
        return customUserList;
    }

}
